import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math' as math;
String tiaozhuan = '';
class home extends StatefulWidget {
  const home({Key? key}) : super(key: key);

  @override
  State<home> createState() => _homeState();
}

class _homeState extends State<home> {
  @override
  List beijintu = [
    'assets/beijin/jiazai.png',
  ];
  String ye = '加载界面';///页面选择
  Timer ?timer1;///开启事件定时器
  int miao = 0;///创建一个时间计数器
  double jiazailength = -120;///加载条的位置
  bool jiazaitiao = true;///加载条是否显示
  List dongtaijiazai = [
    'assets/jiazaidonghua/1.png',
    'assets/jiazaidonghua/2.png',
    'assets/jiazaidonghua/3.png',
    'assets/jiazaidonghua/4.png',
    'assets/jiazaidonghua/5.png',
    'assets/jiazaidonghua/6.png',
    'assets/jiazaidonghua/7.png',
    'assets/jiazaidonghua/8.png',
    'assets/jiazaidonghua/9.png',
    'assets/jiazaidonghua/10.png',
    'assets/jiazaidonghua/11.png',
    'assets/jiazaidonghua/12.png',
    'assets/jiazaidonghua/13.png',
  ];///动态加载图片的列表
  int dongtaijiazaidonghua = 0;///动态加载动画的图片列表位置
  bool dongtaijiazaixianshi = false;///动态加载动画是否显示
  bool xuanquxianshi = false;///选区是否显示
  List suijinameliebiao =[
    '聪明的甘蔗',
    '爱冒险的玉米',
    '萌萌的卷心菜',
    '爱揍僵尸的菜问',
    '勇敢的豌豆',
    '温柔的寒冰射手',
    '胆怯的蘑菇',
  ];///这里是游客名字大全
  String suijiname = '';///这里是游客名称
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      if(suijiname==''){
        suijiname = suijinameliebiao[math.Random().nextInt(7)];
      }
    });
    timer1=Timer.periodic(Duration(milliseconds:50 ), (timer1) {
      setState(() {
       print((miao/20).toInt());
        miao++;
        dongtaijiazaidonghua++;
        if((miao/20).toInt()==2){
          jiazailength=-90;
        }
        if((miao/20).toInt()==3){
          jiazailength=0;
        }
        if((miao/20).toInt()==4){
          dongtaijiazaixianshi=true;
          jiazaitiao =false;
        }
       if((miao/20).toInt()==5){
         dongtaijiazaixianshi=false;
         xuanquxianshi =true;
       }
       if(tiaozhuan == '第一次游戏' &&  (miao/20).toInt() ==12){
         dongtaijiazaixianshi=false;
         Navigator.pushNamed(context, 'game');
         timer1.cancel();
       }
      });
    });
  }
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width/1080;
    double ww = MediaQuery.of(context).size.width;
    double hh = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Container(
        width: ww,
        height: hh,
        color: Colors.black,
        child: Stack(
          children: [
            Visibility(
                visible: ye=='加载界面'?true:false,
                child: Stack(
                  children: [
                    Container(
                      width: ww,
                      height: hh,
                      child: Image.asset(
                          beijintu[0],fit: BoxFit.cover,
                      ),
                    ),
                    Visibility(
                      visible: jiazaitiao,
                      child: Positioned(
                          left: ww/2.37,
                          top: 515*rpx,
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(10*rpx),
                            child: Container(
                              alignment: Alignment.center,
                              width: 180*rpx,
                              height:20*rpx,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8*rpx),
                                  border: Border.all(width: 1*rpx,color: Colors.black45),
                                  color: Color.fromRGBO(0, 0, 0, 0.7)
                              ),
                              child:Stack(
                                children: [
                                  AnimatedPositioned(
                                    left: jiazailength,
                                    duration: Duration(milliseconds: 260),
                                    child: Container(
                                      alignment: Alignment.center,
                                      width: 180*rpx,
                                      height:20*rpx,
                                      decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(8*rpx),
                                          color: Color(0xff52e63a)
                                      ),
                                    ),
                                  )
                                ],
                              ) ,
                            ),
                          )
                      ),
                    ),
                    Visibility(
                      visible: dongtaijiazaixianshi,
                      child: Positioned(
                        left: 520*rpx,
                        top: 300*rpx,
                        child: Container(
                          width: 50*rpx,
                          height: 50*rpx,
                          decoration: BoxDecoration(
                              image: DecorationImage(
                                  image: AssetImage(dongtaijiazai[dongtaijiazaidonghua%dongtaijiazai.length])
                              )
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: xuanquxianshi,
                      child: Positioned(
                          left: ww/2.5,
                          top: 445*rpx,
                          child:Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                alignment: Alignment.center,
                                width: 240*rpx,
                                height:40*rpx,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(3*rpx),
                                    border: Border.all(width: 1*rpx,color: Colors.black54),
                                    color: Colors.white
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Container(
                                      alignment: Alignment.center,
                                      width: 170*rpx,
                                      child: Text(suijiname,style: TextStyle(height: 1.3),),
                                    ),
                                    Icon(Icons.person,color:Color(0xffddb659),size: 30*rpx,),
                                    GestureDetector(
                                      onTap: (){
                                        setState(() {
                                          suijiname=suijinameliebiao[math.Random().nextInt(7)];
                                        });
                                      },
                                      child: Container(
                                        margin: EdgeInsets.only(top: 3*rpx),
                                        width: 30*rpx,
                                        height: 30*rpx,
                                        child:  Icon(Icons.sync,size: 25*rpx,),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              Container(
                                margin: EdgeInsets.only(top: 7),
                                width: 120*rpx,
                                height: 40*rpx,
                                child: ElevatedButton(
                                  style: ButtonStyle(
                                      backgroundColor: MaterialStateProperty.all(Color(0xff7046c4)),
                                      padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                                      side: MaterialStateProperty.all(BorderSide(width: 2*rpx,color: Colors.white))
                                  ),
                                  onPressed: (){
                                    setState(() {
                                      dongtaijiazaixianshi = true;
                                      miao = 10*20;
                                      tiaozhuan = '第一次游戏';
                                    });
                                  },
                                  child: Text('开始游戏',style: TextStyle(fontSize: 20*rpx,fontWeight: FontWeight.w600,height: 1.3),),
                                ),
                              )
                            ],
                          )
                      ),
                    ),
                  ],
                )
            ),
          ],
        ),
      )
    );
  }
}


